package rexsee.natives;

import rexsee.core.browser.Browser;
import rexsee.dialog.ImageMultiTouchListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class ViewOnTouchListener implements OnTouchListener {

	private final Browser mBrowser;
	private final String mId;
	private final boolean mTouchMove;
	private final GestureDetector mGestureDetector;
	private final ImageMultiTouchListener mMultiTouchListener;

	public ViewOnTouchListener(Browser browser, String id, boolean touchMove, ImageMultiTouchListener imageMultiTouchListener) {
		mBrowser = browser;
		mTouchMove = touchMove;
		mId = id;
		mGestureDetector = new GestureDetector(new ViewGestureListener(mBrowser, id));
		mMultiTouchListener = imageMultiTouchListener;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction();
		String[] argu = new String[]{mId, String.valueOf(event.getX()), String.valueOf(event.getY())};
		if (action == MotionEvent.ACTION_DOWN) {
			mBrowser.eventList.run(RexseeJavaView.EVENT_ONJAVAVIEWTOUCHDOWN, argu);
		} else if (action == MotionEvent.ACTION_UP) {
			mBrowser.eventList.run(RexseeJavaView.EVENT_ONJAVAVIEWTOUCHUP, argu);
		} else if (action == MotionEvent.ACTION_MOVE && mTouchMove) {
			mBrowser.eventList.run(RexseeJavaView.EVENT_ONJAVAVIEWTOUCHMOVE, argu);
		} else if (action == MotionEvent.ACTION_CANCEL) {
			mBrowser.eventList.run(RexseeJavaView.EVENT_ONJAVAVIEWTOUCHCANCEL, argu);
		}
		if (mMultiTouchListener != null) mMultiTouchListener.onTouch(v, event);
		return mGestureDetector.onTouchEvent(event);
	}

}
